#include<iostream>
#include<vector>
using namespace std;
const int N = 3e5 + 10;
typedef pair<int, int> PII;
int f[N], temp[N];
int n, m;
vector<PII>e[N];
int main()
{
	cin >> n >> m;
	while (m--) {
		int a, b, c;
		scanf("%d%d%d", &a, &b, &c);
		e[c].push_back({ a,b });
	}
	for (int i = 1; i <= 1e5; i++) {
		for (PII x: e[i]) {
			temp[x.second] = max(temp[x.second], max(f[x.second], f[x.first] + 1));
		}
		for (PII x : e[i]) {
			f[x.second] = temp[x.second];
		}
	}
	int ma = 0;
	for (int i = 1; i <= n; i++) {
		ma = max(ma, f[i]);
	}
	cout << ma << endl;
	return 0;
}